package com.ferapps.tecsa;

import java.text.MessageFormat;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME="dbTecsa.db";
    private static final int SCHEMA_VERSION=1;

    public DatabaseHelper(Context context) {
	super(context, DATABASE_NAME, null, SCHEMA_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
	String sql="CREATE TABLE {0} ({1} INTEGER PRIMARY KEY AUTOINCREMENT,"
	    + " {2} TEXT NOT NULL,{3} TEXT NOT NULL,{4} TEXT NOT NULL);";
	db.execSQL(MessageFormat.format(sql, PreferitiTable.TABLE_NAME, PreferitiTable._ID, PreferitiTable.IDIMPIANTO,
	    PreferitiTable.DESCRIZIONE, PreferitiTable.COMANDO));
	for (int i=0; i <= 9; i++) {
	    inserisciPreferito(db, "Non impostato", "", "");
	}
    }

    private void inserisciPreferito(SQLiteDatabase db, String descrizione, String comando, String idImpianto) {
	ContentValues v=new ContentValues();
	v.put(PreferitiTable.IDIMPIANTO, idImpianto);
	v.put(PreferitiTable.DESCRIZIONE, descrizione);
	v.put(PreferitiTable.COMANDO, comando);
	db.insert(PreferitiTable.TABLE_NAME, null, v);

    }

    public void aggiornaPreferitiTUTTI() {
	SQLiteDatabase db=getWritableDatabase();
	db.execSQL("update " + PreferitiTable.TABLE_NAME + " set " + PreferitiTable.DESCRIZIONE + " = 'CIAO'"
	/* new Object[]{regione, codice } */);

    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
	// TODO Auto-generated method stub

    }

    public Cursor getPreferiti() {
	return (getReadableDatabase().query(PreferitiTable.TABLE_NAME, PreferitiTable.COLUMNS, null, null, null, null,
	    PreferitiTable._ID));
    }

}